<template>
    <div id="taskList" v-loading="loading">
        <v-tableTitle :btnConfig="btnConfig"
                      :tableSelectData="tableSelectData"
                      :title="$t('other.taskList')"
                      @del="del" @newCreate="newCreate"
                      @suspend="suspend" @recover="recover"></v-tableTitle>
        <v-table
            :pageSize="pageSize"
            :page="page"
            :tableConfig="tableConfig"
            :tableData="tableData"
            :total="total"
            @findData="getTableData"
            @getTableData="getTableData"
            @searhData="searhData"
            @selectData="selectData"
            @setdialog="setdialog"
            @upData="getTableData"
            ref="table"
        ></v-table>
        <el-dialog :visible.sync="dialogFormVisible" v-dialogDrag :modal="false"
                   :close-on-click-modal="false">
            <v-details :inputConfig="inputConfig" :detailData="inputData" :rules="rules" ref="input"></v-details>
            <div slot="footer" class="dialog-footer">
                <el-button :disabled="!this.btnPermNameList.includes('EN040105')" @click="dialogclose">{{$t('common.cancel')}}</el-button>
                <el-button :disabled="!this.btnPermNameList.includes('EN040106')" type="primary" @click="confirm">{{$t('common.confirm')}}</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script type="text/ecmascript-6">
import tableTitle from "commons/TableTitle";
import table from "commons/Table";
import baseMixin from "@/mixins/BaseMixin";
import config from "./Config";
import details from 'commons/Detail'

export default {
    name: "taskList",
    mixins: [baseMixin, config],
    data() {
        return {
            loading: false,
            tableConfig: [
                {
                    label: this.$t('other.job_name'),
                    prop: "job_name",
                    input: "input",
                },
                {
                    label: this.$t('other.job_group'),
                    prop: "job_group",
                    input: "input",
                },
                {
                    label: this.$t('other.job_class_name'),
                    prop: "job_class_name",
                    input: "input",
                },
                {
                    label: this.$t('other.trigger_name'),
                    prop: "trigger_name",
                    input: "input",
                },
                {
                    label: this.$t('other.trigger_group'),
                    prop: "trigger_group",
                    input: "input",
                },
                {
                    label: this.$t('other.cron_expression'),
                    prop: "cron_expression",
                    input: "input",
                },
                {
                    label: this.$t('common.status'),
                    prop: "trigger_state",
                    input: "input",
                },
                {
                    label: this.$t('other.time_zone_id'),
                    prop: "time_zone_id",
                    input: "input",
                }
            ],
            tableData: [],
            pageSize: 10,
          page: 1,
            total: 0,
            tableSelectData: [],
            tableSearhData: {},
            wms: '',
            dialogFormVisible: false,
            rules: {},
            inputConfig: [],
            url: "",
            inputData: {},
        };
    },
    computed: {
        btnConfig() {
            return [
                {label: this.$t('common.create'), certigier: !this.btnPermNameList.includes('EN040101'), invalid: "newCreate"},
                {label: this.$t('common.delete'), certigier: !this.btnPermNameList.includes('EN040102'), disabled: true, invalid: "del"},
                // {label: "修改", disabled: true, invalid: "freeze"},
                {label: this.$t('out.suspend'), certigier: !this.btnPermNameList.includes('EN040103'), disabled: true, invalid: "suspend"},
                {label: this.$t('other.recover'), certigier: !this.btnPermNameList.includes('EN040104'), disabled: true, invalid: "recover"},
            ]
        }
    },
    created() {
    },
    methods: {
        async suspend(data) {
            try {
                let res = await this.$api.post(`/pausejob`, data);
                
                    this.$message.success(res.returnMgr);
                    let page = sessionStorage.getItem('page');
                    this.getTableData(page, this.tableSearhData);
                    
                
            } catch (e) {
                this.$message.error(e);
            }
        },
        async recover(data) {
            try {
                let res = await this.$api.post(`/resumejob`, data);
                
                    this.$message.success(res.returnMgr);
                    let page = sessionStorage.getItem('page');
                    this.getTableData(page, this.tableSearhData);
                    
                
            } catch (e) {
                this.$message.error(e);
            }
        },
        //新建
        async newCreate() {
            this.inputConfig = [
                [
                    {
                        type: "input",
                        label: this.$t('other.job_class_name'),
                        props: "job_class_name",
                    }
                ],
                [
                    {
                        type: "input",
                        label: this.$t('other.job_group'),
                        props: "job_group",
                    }
                ],
                [
                    {
                        type: "input",
                        label: this.$t('other.cron_expression'),
                        props: "cron_expression",
                    }
                ],
            ];
            this.inputData = {
                job_class_name: "",
                job_group: "",
                cron_expression: "",
            };
            this.url = "/addjob";
            this.dialogFormVisible = true;
            // this.$store.commit("setNewFlag", true);
            // this.$router.push({name: 'taskListDeawer'});
        },
        async confirm() {
            try {
                let res = await this.$api.post(this.url, this.inputData);
                
                    this.inputConfig = [];
                    this.inputData = {};
                    this.url = "";
                    this.dialogFormVisible = false;
                    let page = sessionStorage.getItem('page');
                    this.getTableData(page, this.tableSearhData);
                    this.$message.success(res.returnMgr);
                
            } catch (e) {
                this.$message.error(e);
            }
        },
        dialogclose() {
            this.inputConfig = [];
            this.inputData = {};
            this.url = "";
            this.dialogFormVisible = false;
        },
        //选中的内容
        selectData(data) {
            this.tableSelectData = data;
        },
        //删除
        async del(data) {
            try {
                let res = await this.$api.post(`/deletejob`, data);
                
                    this.$message.success(res.returnMgr);
                    let page = sessionStorage.getItem('page');
                    this.getTableData(page, this.tableSearhData);
                    
                
            } catch (e) {
                this.$message.error(e);
            }
        },
        async getTableData(page, data) {
            try {
                this.loading = true;
                let res = await this.$api.post(`/queryjob/${page}`, data);
                
                    this.tableData = res.page.list;
                    this.pageSize = res.page.pageSize;
                  this.page = res.page.curPage;
                    this.total = res.page.total;
                
            } catch (e) {
                this.$message.error(e);
            } finally {
                this.loading = false;
            }
        },
        //搜索的值
        searhData(data, up) {
            if (!up) {
                this.tableSearhData = data;
            } else {
                this.tableSearhData = data;
                this.getTableData(1, this.tableSearhData);
            }
        },
        //设置弹框
        setdialog(data) {
            /*this.$store.commit("setRouteData", data);
            this.$router.push({name: 'taskListDeawer'});*/
            this.inputConfig = [
                [
                    {
                        type: "textarea",
                        label: this.$t('other.cron_expression'),
                        props: "cron_expression",
                    }
                ],
            ];
            this.inputData = data;
            this.url = "/rescheduleJob";
            this.dialogFormVisible = true;
        },
    },
    mounted() {
        
        this.wms = this.$store.state.wms;
        this.$store.commit("setSkip", {});
        this.$store.commit("setNewFlag", false);
        this.$store.commit("setRouteData", {});
        this.getTableData(1, {});
    },
    watch: {
        tableSelectData(data) {
            if (this.permissionStatus) {
                if (data.length > 0) {
                    this.deleteStatus = false;
                } else {
                    this.deleteStatus = true;
                }
            }
        }
    },
    components: {
        "v-tableTitle": tableTitle,
        "v-table": table,
        'v-details': details
    }
};
</script>

<style scoped>
#taskList {
    width: 100%;
    height: 99%;
    padding-top: 6px;
}
</style>
